package com.dxy.dxyadmin.controller;

import com.dxy.dxyadmin.DO.Admin;
import com.dxy.dxyadmin.DO.Permission;
import com.dxy.dxyadmin.DTO.PermissionTree;
import com.dxy.dxyadmin.VO.AdminVO;
import com.dxy.dxyadmin.enums.ResultEnum;
import com.dxy.dxyadmin.service.AdminService;
import com.dxy.dxycommon.VO.ResultVO;
import com.dxy.dxycommon.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * junfeng 2018/11/6 管理员控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 添加管理员
     *
     * @param adminVO
     * @return
     */
    @RequestMapping(value = "/insertAdmin", consumes = "application/json")
    public ResultVO insertAdmin(@RequestBody @Validated AdminVO adminVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        Admin admin = adminService.insertAdmin(adminVO);
        if (admin == null) {
            return ResultVOUtil.error(ResultEnum.USER_EXIST);
        }
        return ResultVOUtil.success(admin);
    }

    /**
     * 更新管理员信息
     *
     * @param adminVO
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/updateAdmin", consumes = "application/json")
    public ResultVO updateAdmin(@RequestBody @Validated AdminVO adminVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        Admin admin = adminService.updateAdmin(adminVO);
        return ResultVOUtil.success(admin);
    }

    /**
     * 更新管理员账号状态
     *
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "/updateStatusAdmin", consumes = "application/json")
    public ResultVO updateStatusAdmin(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        int adminInfo = adminService.updateStatusAdmin(id, status);
        return ResultVOUtil.success(adminInfo);
    }

    /**
     * 管理员列表
     *
     * @param params
     * @return
     */
    @RequiresPermissions("admin:view")
    @RequestMapping(value = "/listAdmin")
    public ResultVO listAdmin(@RequestParam Map<String, Object> params) {
        log.info(params.toString());
        Page<Admin> adminInfo = adminService.listAdmin(Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("size").toString()), params);
        return ResultVOUtil.success(adminInfo);
    }

    /**
     * 查询侧边栏
     *
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/querySide")
    public ResultVO querySide() {
        Subject subject = SecurityUtils.getSubject();
        AdminVO admin = (AdminVO) subject.getSession().getAttribute("admin");
        List<PermissionTree> permissionTreeList = adminService.querySide(admin.getName());
        return ResultVOUtil.success(permissionTreeList);
    }

    @CrossOrigin
    @RequestMapping(value = "/queryBtn")
    public ResultVO queryBtn(String title) {
        Subject subject = SecurityUtils.getSubject();
        AdminVO admin = (AdminVO) subject.getSession().getAttribute("admin");
        List<Permission> permissionTreeList = adminService.queryBtn(admin.getName(),title);
        return ResultVOUtil.success(permissionTreeList);
    }




}
